package com.example.listviewedittext;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView list= (ListView) findViewById(R.id.list);
        List<String> lists=new ArrayList<>();
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        lists.add("dddddd");
        myAdapter my=new myAdapter(lists,this);
//        list.requestFocus();
//        list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                list.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
//
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//            }
//        });
        list.setAdapter(my);



    }



    class myAdapter extends BaseAdapter{
        private List<String> strings;
        private LayoutInflater lf;

        public myAdapter(List<String> strings, Context context) {
            this.strings = strings;
            lf= LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return strings.size();
        }

        @Override
        public Object getItem(int position) {
            return strings.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemViewType(int position) {
            if (position==0)
                return 1;
            return 2;
        }

        @Override
        public int getViewTypeCount() {
            return 3;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            int type=getItemViewType(position);
            TextView text = null;

            EditText tt = null;
            if (convertView==null) {
                if (type==1){
                convertView = lf.inflate(R.layout.item_edit, null);
                 tt= (EditText) convertView.findViewById(R.id.editText);
                    tt.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                            notifyDataSetChanged();
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
//                            notifyDataSetChanged();
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
//                            notifyDataSetChanged();

                            Log.d("贱毛病",s.toString());
                        }
                    });
                }else if (type==2){
                    convertView = lf.inflate(R.layout.item_text, null);
                     text= (TextView) convertView.findViewById(R.id.textView);

                }



            }else {

                if (type==1){

                    tt= (EditText) convertView.findViewById(R.id.editText);
                    tt.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                            notifyDataSetChanged();
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
//                            notifyDataSetChanged();
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            notifyDataSetChanged();
                        }
                    });
                }else if (type==2){
                    text= (TextView) convertView.findViewById(R.id.textView);

                }


            }
            if (text!=null)
            text.setText(strings.get(position));



            return convertView;
        }
    }

}


